package com.weme.mine.activity

import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.media3.common.MediaItem
import androidx.media3.exoplayer.ExoPlayer
import androidx.media3.ui.PlayerView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.QuickViewHolder
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.weme.common.util.gone
import com.weme.common.util.loadImage
import com.weme.common.util.visible
import com.weme.common.arouter.ARouterPaths
import com.weme.common.base.BaseMvvmActivity
import com.weme.mine.R
import com.weme.mine.bean.AlbumBean
import com.weme.mine.databinding.ActivityPreviewBinding
import com.weme.mine.viewmode.MineViewMode
import dev.kratos.api.user_server.v1.Media

@Route(path = ARouterPaths.MINE_PREVIEW_ACTIVITY)
class PreViewActivity : BaseMvvmActivity<ActivityPreviewBinding, MineViewMode>() {
    lateinit var player: ExoPlayer
    var items = mutableListOf<Media>()

    override fun initView(savedInstanceState: Bundle?) {
        ImmersionBar.with(this).hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR).titleBar(mBinding.customTopNavigationBar).init()
        player = ExoPlayer.Builder(this).build()
        val layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        mBinding.rcvList.layoutManager = layoutManager
        mBinding.rcvList.adapter = preViewAdapter

        mBinding.vp2.adapter = bannerAdapter
        val media = Media.newBuilder().setType(1).setUrl("https://backups3d.s3.ap-east-1.amazonaws.com/chatchat/album/anomol/anmol_1.jpg").build()
        val media1 = Media.newBuilder().setType(2).setUrl("https://backups3d.s3.ap-east-1.amazonaws.com/chatchat/album/anmol/anmol.mp4.mp4").build()

        items.add(media)
        items.add(media1) //                myVideoView.getImageView().loadImage("https://backups3d.s3.ap-east-1.amazonaws.com/chatchat/album/anomol/anmol_1.jpg")
        //                myVideoView.setVideoUrl("https://backups3d.s3.ap-east-1.amazonaws.com/chatchat/album/anmol/anmol.mp4.mp4")
    }

    private val preViewAdapter = object : BaseQuickAdapter<AlbumBean, QuickViewHolder>() {

        override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: AlbumBean?) {


        }

        override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): QuickViewHolder {
            return QuickViewHolder(R.layout.item_preview, parent)
        }


    }


    override fun getViewModelClass(): Class<MineViewMode> = MineViewMode::class.java

    

    private val bannerAdapter = object : RecyclerView.Adapter<BannerViewHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BannerViewHolder {
            val inflater = LayoutInflater.from(parent.context)
            val itemView = inflater.inflate(R.layout.banner_item_layout, parent, false)
            return BannerViewHolder(itemView)
        }

        override fun onBindViewHolder(holder: BannerViewHolder, position: Int) {
            val realPosition = position % items.size
            val item = items[realPosition]
            if (item.type == 1) { // 显示图片
                holder.imageView.visibility = View.VISIBLE
                holder.videoView.visibility = View.GONE
                holder.imageView.loadImage(item.url)
                holder.imPlay.gone()

            } else { // 显示视频
                holder.imageView.visibility = View.GONE
                holder.videoView.visibility = View.VISIBLE
                holder.imPlay.visible()

                holder.videoView.visibility = View.VISIBLE // 将 ExoPlayer 与 PlayerView 关联
                holder.videoView.player = player

                // 创建 MediaSource，用于指定视频的来源（可以是网络地址或本地文件）
                val videoUri: Uri = Uri.parse(item.url)
                // 创建一个Seek参数


                // 设置Seek参数
              //  player.setSeekParameters(SeekParameters.EXACT)
                val mediaItem = MediaItem.fromUri(videoUri)
                player.setMediaItem(mediaItem) // 准备播放器
                player.prepare()

            }
        }


        override fun getItemCount(): Int { // 返回一个非常大的数，例如 Int.MAX_VALUE
            return Int.MAX_VALUE
        }


    }

    inner class BannerViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageView: ImageView = itemView.findViewById(R.id.imageview)
        val videoView: PlayerView = itemView.findViewById(R.id.styledPlayerView)
        val imPlay: ImageView = itemView.findViewById(R.id.imPlay)

    }

}